package cn.admin.controller;

import cn.admin.config.web.ResponseMessage;
import cn.admin.config.web.ResponseStatusCode;
import cn.admin.pojo.Admin;
import cn.admin.pojo.dto.AdminDTO;
import cn.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/24
 * Time: 20:59
 * Description:
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/delete")
    public ResponseMessage delete(@RequestParam("userId") long userId) {
        final boolean remove = adminService.remove(userId);
        if (remove) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "删除成功", "");
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "删除失败", "");
    }

    @PostMapping("/getAdminInfo")
    public ResponseMessage getAdminInfo(@RequestParam("userId") long userId) {
        AdminDTO adminDTO = adminService.find(userId);
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", adminDTO);
    }

    @PostMapping("/update")
    public ResponseMessage update(Admin admin) {
        boolean modify = adminService.modify(admin);
        if (modify) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "修改成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "修改失败", null);
    }

    @PostMapping("/save")
    public ResponseMessage save(Admin admin) {
        boolean insert = adminService.add(admin);
        if (insert) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "添加成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "添加失败", null);
    }

}
